package com.fumin.util;

import java.util.Date;
import java.util.Random;

/**
 * 密码生成类
 * 不容易识别的字符这里会被排除掉，例如：l,1,i,o,0
 * @Author xiayuxuanmin
 * @Date 2019/10/25
 * @Description
 */
public class PasswordUtil {
    public final static String[] word = {
            "a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z",
    };

    public final static String[] num = {
            "2","3","4","5","6","7","8","9",
    };

    /**
     * 生成随机的密码，以数字和字母组合
     * @return
     */
    public static String randomPassword(){
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random(new Date().getTime());
        boolean flag = false;//为TRUE，则取数字；为FALSE，则取字母
        int length = random.nextInt(3)+8;//设定密码在8~11之间，可以为8位，9位，或10位
        for (int i = 0; i < length; i++) {
            if (flag) {
                stringBuffer.append(num[random.nextInt(num.length)]);
            }else{
                stringBuffer.append(word[random.nextInt(word.length)]);
            }
            flag = !flag;//这么设定是为了让密码在数字和字母之间变化
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) throws Exception {
        System.out.println(randomPassword());
        Thread.sleep(1000);
        System.out.println(randomPassword());
        Thread.sleep(1000);
        System.out.println(randomPassword());
    }
}
